<template>
	<view>
		<cu-custom bgColor="bg-black" :isBack="false">
			<block slot="right"><text @tap='tomy' style="margin-left: 40rpx; " class="cuIcon-back"></text>
				<view class="action">
					<button class="cu-btn round bg-white" @tap="submit">修改</button>
				</view>
			</block>
			<block slot="content">修改密码</block>
		</cu-custom>
		<form>
			<view class="cu-form-group margin-top">
				<view class="title">原密码</view>
				<input :password="true" v-model="oldpass"></input>
			</view>

			<view class="cu-form-group">
				<view class="title">新密码</view>
				<input placeholder="" :password="true" v-model="newpass"></input>
			</view>
			<view class="cu-form-group">
				<view class="title">确认密码</view>
				<input placeholder="" :password="true" v-model="renew"></input>
			</view>
		</form>

	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				isRotate: false, //是否加载旋转
				oldpass: '',
				newpass: '',
				renew: '',
				userInfo: {
					ext_info: {
						yesapi_avatar: "",
						yesapi_email: "",
						yesapi_expiration_time: "",
						yesapi_mobile: "",
						yesapi_nickname: "",
						yesapi_points: 0,
						yesapi_real_name: "",
						yesapi_reg_note: "",
						yesapi_reg_source: "",
						yesapi_sex: ""
					},
					register_ip: "",
					register_time: "",
					role: "",
					rolename: "",
					status: 0,
					username: "",
					uuid: "",
					token: ""
				},


			};
		},
		mounted() {

			var info = this.checkLogin("home", '2');
			if (info) {
				this.userInfo = info;
			}


		},
		methods: {
			...mapMutations(['login', 'logout']),
			tomy() {
				this.$store.commit("setData", {
					key: "tabpage",
					val: 'my'
				});
				uni.reLaunch({
					url: "../index"
				});
			},
			async submit() {
				let util = require('../../components/okayapi/md5.js')

				//判断
				if (this.oldpass == "" || this.newpass == "" || this.renew == "") {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '所有输入框不能为空'
					});
					return;
				}
				if (this.oldpass.length < 6 || this.newpass.length < 6 || this.renew.length < 6) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '密码至少6位'
					});
					return;
				}
				if (this.newpass != this.renew) {
					uni.showToast({
						icon: 'none',
						position: 'bottom',
						title: '新密码和确认密码不一致'
					});
					return;
				}



				this.isRotate = true
				setTimeout(function() {
					this.isRotate = false
				}, 3000)
				uni.showLoading({
					title: '更新中...'
				});

				var options = {
					data: {
						s: "App.User.AlterPassword",
						username: this.userInfo.username,
						old_password: util.hexMD5(this.oldpass).toLowerCase(),
						new_password: util.hexMD5(this.newpass).toLowerCase(),
					},
					method: "POST"
				}
				const res = await this.$myRuquest(options)
				if (res.data.data.err_code == 0) {
					this.logout()
					uni.hideLoading();
					uni.showToast({
						icon: 'success',
						position: 'bottom',
						title: '更新成功'
					});
					this.$store.commit("setData", {
						key: "tabpage",
						val: 'my'
					});
					uni.reLaunch({
						url: "/pages/index"
					});

				} else {
					uni.showToast({
						icon: '',
						position: 'bottom',
						title: '更新失败:' + res.data.data.err_msg
					});
					uni.reLaunch({
						url: "/pages/index"
					});

				}

			},
			changeEmail(e) {
				this.userInfo.ext_info.yesapi_email = e.detail.value;

			},
			changeNickname(e) {
				this.userInfo.ext_info.yesapi_nickname = e.detail.value;

			},

			PickerChange(e) {
				switch (e.detail.value) {
					case 0:
						this.userInfo.ext_info.yesapi_sex = '';
						break;
					case 1:
						this.userInfo.ext_info.yesapi_sex = 'male';
						break;
					case 2:
						this.userInfo.ext_info.yesapi_sex = 'fmale';
						break;
				}
			},
		}
	}
</script>

<style>
	.cu-form-group .title {
		min-width: calc(4em + 15px);
	}
</style>
